<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends CI_Controller {
    function Comment() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
    }
		
    function insert(){
        $data = array(
            'car_id' => $this->input->post('car_id', TRUE),
            'comment_car' => $this->input->post('comment_car', TRUE),
            'member_id' => $this->session->userdata('userid'),
            'series_id' => 0);
    
        $this->db->insert('psacar_comment',$data);
        $session_id = $this->session->userdata('userid');
        redirect(base_url() . 'index.php/car/detail/' . $_POST['car_id'],$data);			
    }
    
    public function result()
    {
        $data['message'] = "";

        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;                
        
        $this->load->model('Car_model');
        $car_list = $this->Car_model->get_car_list();

        //Pagination
        $data['pagination'] = $this->generate_pagination(
            'index.php/comment/result',
            $car_list->num_rows,
            $limit);
        
        $car_list = array_slice($car_list->result(), $offset, $limit);
        $data['car_list'] = $car_list;
        
        $this->load->view('header', $data);
        $this->load->view('templates/car/car_search_result', $data);
        
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);
    }
    
    public function detail()
    {
        $data['message'] = "";
        $car_id = $this->uri->segment(3);        

        $this->load->model('Car_model');
        //$this->db->order_by('total_points', 'Desc');
        $data['car_list'] = $this->Car_model->get_car_by_id($car_id);
        
        $this->load->model('Comment_model');
        $data['comment_list'] = $this->Comment_model->get_comments_by_car($car_id);	
        
        $this->load->view('header',$data);
        $this->load->view('templates/car/car_detail', $data);
        
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);        
    }	
    
    public function generate_pagination($base_url = '', $total_row = 0, $per_page = 0) {       
        $this->load->library('pagination');
        $config['base_url'] = base_url() . $base_url;
        $config['total_rows'] = $total_row;
        $config['per_page'] = $per_page;

        $limiter = '<select name="limit">';
        for($counter = 10; $counter <= 100; ) {
            $limiter .= 
                '<option value="' . $counter . '" ' . 
                ($per_page == $counter ? 'selected' : '') . 
                '>' . $counter . ' per page</option>';
            $counter += 10;
        }
        $limiter .= "</select> | ";
                
        $config['full_tag_open'] = '<div class="pagination">' . $limiter;
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }        
}